package com.lianxian.xindong.commonres.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hxb.library.utils.DeviceUtils;
import com.lianxian.xindong.commonres.R;

/**
 * @作者： HeXingBo
 * @时间： 2022/11/21 16:09
 * @描述： 通用提示弹窗仿IOS样式
 */
public class MyHintDialog extends Dialog {
    private TextView tvTitle, tvContent, btnClose, btnSubmit;
    private OnDialogListener listener;
    private String mTitle = getContext().getString(R.string.public_dialog_hint_title),
            mContent = getContext().getString(R.string.public_dialog_sure_action),
            mLeft = getContext().getString(R.string.public_dialog_cancel),
            mRight = getContext().getString(R.string.public_dialog_ok);
    private View viewLineV, viewLineH;


    public MyHintDialog(@NonNull Context context) {
        super(context, R.style.public_my_hint_dialog);
        initView("", "", "", "");
    }

    public MyHintDialog(@NonNull Context context, String title, String content, String leftValue, String rigthValue) {
        super(context, R.style.public_my_hint_dialog);
        initView(title, content, leftValue, rigthValue);

    }

    private void initView(String title, String content, String leftValue, String rigthValue) {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.public_dialog_hint, null);
        LinearLayout layout = contentView.findViewById(R.id.ll_root);
        //取控件当前的布局参数
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) layout.getLayoutParams();
        //设置宽度值
        params.width = (int) DeviceUtils.getScreenWidth(getContext()) - (int) DeviceUtils.dpToPixel(getContext(), 80);
        //设置高度值
        //params.height = HxbUtils.dip2px(getContext(), height);
        //使设置好的布局参数应用到控件
        layout.setLayoutParams(params);

        viewLineV = contentView.findViewById(R.id.viewLineV);
        viewLineH = contentView.findViewById(R.id.viewLineH);
        tvTitle = contentView.findViewById(R.id.tv_title);
        tvContent = contentView.findViewById(R.id.tv_hint);
        btnClose = contentView.findViewById(R.id.tv_cancel);
        btnSubmit = contentView.findViewById(R.id.tv_ok);
        tvTitle.setText(TextUtils.isEmpty(title) ? mTitle : title);
        tvContent.setText(TextUtils.isEmpty(content) ? mContent : content);
        btnClose.setText(TextUtils.isEmpty(leftValue) ? mLeft : leftValue);
        btnSubmit.setText(TextUtils.isEmpty(rigthValue) ? mRight : rigthValue);

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onItemViewLeftListener();
                }
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemViewRightListener();
                }
            }
        });
        setContentView(contentView);

//        setCanceledOnTouchOutside(false);
//        setOnKeyListener(new OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
//                    return true;
//                }
//                else{
//                    return false;
//                }
//            }
//        });
    }

    public void setVisibileBottomView(boolean isVisibileLeft) {
        btnClose.setVisibility(isVisibileLeft ? View.VISIBLE : View.GONE);
        btnSubmit.setVisibility(isVisibileLeft ? View.GONE : View.VISIBLE);
    }

    public void setTextTitle(String title) {
        tvTitle.setText(TextUtils.isEmpty(title) ? mTitle : title);
    }

    public void setTextContent(String content) {
        tvContent.setText(TextUtils.isEmpty(content) ? mContent : content);
    }

    public void setBtnClose(String leftValue) {
        btnClose.setText(TextUtils.isEmpty(leftValue) ? mLeft : leftValue);
    }

    public void setBtnSubmit(String rigthValue) {
        btnSubmit.setText(TextUtils.isEmpty(rigthValue) ? mRight : rigthValue);
    }

    public void setTitleVisibility(boolean show) {
        tvTitle.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setRightVisibility(boolean show) {
        btnSubmit.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setLeftVisibility(boolean show) {
        btnClose.setVisibility(show ? View.VISIBLE : View.GONE);
        viewLineV.setVisibility(show ? View.VISIBLE : View.GONE);
//        viewLineH.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setOnDialogListener(OnDialogListener listener) {
        this.listener = listener;
    }

    public abstract static class OnDialogListener {
        public void onItemViewLeftListener() {
        }

        public abstract void onItemViewRightListener();
    }


}
